<%--
  Created by IntelliJ IDEA.
  User: Shuwei  Wang
  Date: 2020/6/18
  Time: 15:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body>

<button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传excel</button>
<a href="${pageContext.request.contextPath}/guru/guruToExcel"><button type="button" class="layui-btn" id="download" >批量下载</button></a>
<button class="layui-btn" id="toAddGuru">添加上师</button>
<form class="layui-form" action="" id="addGuruForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">上师真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="guruName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上师法号</label>
        <div class="layui-input-block">
            <input type="text" name="guruNickname" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <button type="button" class="layui-btn" id="test1">
        <i class="layui-icon">&#xe67c;</i>上传上师照片
    </button>
    <img src="" id="guruImage" >
    <input type="hidden" name="guruImage" >

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="guruStatus" value="1" title="是">
            <input type="radio" name="guruStatus" value="0" title="否">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submitguru">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<%--修改上师--%>
<form class="layui-form" action="" lay-filter="update" id="updGuruForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">上师真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="guruName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
        <input type="hidden"  name="guruId"/><%--编号隐藏--%>
    <div class="layui-form-item">
        <label class="layui-form-label">上师法号</label>
        <div class="layui-input-block">
            <input type="text" name="guruNickname" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <button type="button" class="layui-btn" id="updtest1">
        <i class="layui-icon">&#xe67c;</i>修改上师照片
    </button>
    <img src="" id="updguruImage" >
    <input type="hidden" name="guruImage" >

    <div class="layui-form-item">
    <label class="layui-form-label">是否显示</label>
    <div class="layui-input-block">
        <input type="radio" name="guruStatus" value="1" title="是" id="shi"/>
        <input type="radio" name="guruStatus" value="0" title="否" id="fou"/>
    </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updguru">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test" ></table>
<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
<script>
    //Demo
    layui.use(['form','table','layer','upload'], function(){
        var form = layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var upload=layui.upload;
        var index;
        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '${pageContext.request.contextPath}/guru/allGuruPage' //数据接口
            ,limit:3
            ,limits:[3,6]
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'guruId', title: '上师编号',  sort: true/*, fixed: 'left'*/}
                ,{field: 'guruName', title: '上师名字'}
                ,{field: 'guruStatus', title: '上师状态',  sort: true}

                ,{field: 'guruNickname', title: '上师法号'}
                ,{field:'guruImage',title: '上师照片',  sort: true, width:224,height:224,templet:function (data) {
                        console.log(data.guruImage)
                        return "<img src='${pageContext.request.contextPath}/img/"+data.guruImage+"'/>";
                    }
                }
                ,{title: '操作',toolbar: '#barDemo'}

            ]]
        });

        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if(layEvent === 'detail'){ //查看
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"${pageContext.request.contextPath}/guru/delGuru",
                        data:"id="+data.guruId,
                        dataType:"text",
                        type:"post",
                        success:function (d) {
                            if(d=="true"){
                                layer.msg("下架成功！");
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }else{
                                layer.msg("操作失败！");
                                layer.close(index);
                            }
                        }
                    });


                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                $("#updguruImage").prop("src","${pageContext.request.contextPath}/img/"+data.guruImage);
                form.val('update',data)
                <%--$.ajax({--%>
                    <%--url:"${pageContext.request.contextPath}/guru/selOneGuru",--%>
                    <%--data:"id="+data.guruId,--%>
                    <%--dataType:"json",--%>
                    <%--type:"post",--%>
                    <%--success:function (d) {--%>
                        <%--$("[name='guruId']").val(d.guruId);--%>
                        <%--$("[name='guruName']").val(d.guruName);--%>
                        <%--$("[name='guruImage']").val(d.guruImage);--%>
                        <%--$("#updguruImage").prop("src","${pageContext.request.contextPath}/img/"+d.guruImage);--%>
                        <%--$("[name='guruNickname']").val(d.guruNickname);--%>

                        <%--if(d.guruStatus==1){--%>

                            <%--/*$("#shi").prop("checked",true);*/--%>

                            <%--$(document).on("#shi",function () {--%>
                                <%--$(this).prop("checked","checked");--%>
                            <%--});--%>

                        <%--}else {--%>
                            <%--$("#fou").prop("checked",true);--%>
                        <%--}--%>


                    <%--}--%>
                <%--});--%>

              index=layer.open({
                    type:1,
                    area:['500px','500px'],
                    content:$("#updGuruForm"),
                    title:"修改上师信息"
                });
              form.on("submit(updguru)",function (data) {
                 var data=data.field;
                 $.ajax({
                     url:'${pageContext.request.contextPath}/guru/updGuru',
                     data:data,
                     dataType:'text',
                     type:"post",
                     success:function (data1) {
                         if(data1=='true'){
                             layer.msg('修改成功');
                             //同步更新缓存对应的
                            // $("#updGuruForm").get(0).reset();
                            table.reload("demo");

                             layer.close(index);
                         }else {
                             layer.msg('修改失败！');
                         }
                     }
                 })
                  return false;
              });


            } else if(layEvent === 'LAYTABLE_TIPS'){
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });

        //添加上传上师图片
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '${pageContext.request.contextPath}/guru/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                if(res.code==0){
                    $("#guruImage").prop("src","${pageContext.request.contextPath}/img/"+res.src);
                    $("[name='guruImage']").val(res.src);
                    layer.msg("上传成功！");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('上传失败！');
            }
        });

        //修改上传上师图片

        var uploadInst1 = upload.render({
            elem: '#updtest1' //绑定元素
            ,url: '${pageContext.request.contextPath}/guru/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                if(res.code==0){
                    $("#guruImage").prop("src","${pageContext.request.contextPath}/img/"+res.src);
                    $("[name='guruImage']").val(res.src);
                    layer.msg("修改成功！");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('修改失败！');
            }
        });

        $("#toAddGuru").click(function () {
           index= layer.open({
                title:"添加上师",
                area:['400px','400px'],
                content:$("#addGuruForm"),
                type:1
            })
        })
        //监听提交
        form.on('submit(submitguru)', function(data){

            layer.msg(JSON.stringify(data.field));
            var data=data.field;
            $.ajax({
               url:"${pageContext.request.contextPath}/guru/addGuru"
                ,data:data
                ,dataType:"text"
                ,type:"post"
                ,success:function (data) {
                    if(data=="true"){
                        layer.msg("添加上师成功！");
                        layer.close(index);
                    }else{
                        layer.msg("添加失败！");
                    }
                }
            })
            return false;
        });

        /*上传excel变为数据库数据*/
        upload.render({
            elem: '#test3'
            ,url: '${pageContext.request.contextPath}/guru/excelToJava' //改成您自己的上传接口
            ,accept: 'file' //普通文件
            ,done: function(res){
                layer.msg('上传成功');
                console.log(res);
                table.reload("demo");
            }
        });
       /*$("#download").click(function () {
           $.ajax({
               url:'${pageContext.request.contextPath}/guru/guruToExcel'
               ,type:"post",
               dataType:"json",
               success:function () {

               }

           })
        });*/
    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
    {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
    {{#  } }}
</script>

<style type="text/css">
    .layui-table-cell{
        text-align:center;
        height: auto;
        white-space: normal;
    }
</style>
</body>
</html>
